﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yunwei.DAL;
using Yunwei.Domain.Models;
using Yunwei.IBizLogic;

namespace Yunwei.BizLogic
{
    public class RestaurantService : BaseService<Restaurant>, IRestaurantService
    {
        public RestaurantService():base(RepositoryFactory.RestaurantRepository)
        {
        }


        public IQueryable<Restaurant> FindList(int number, string name, int userid, int orderCode)
        {
            return FindPageList(out number, 1, number, name, userid, orderCode);
        }

        public IQueryable<Restaurant> FindPageList(out int totalRecord, int pageIndex, int pageSize, string name, int userID, int orderCode)
        {
            //获取实体列表
            IQueryable<Restaurant> _commonModels = CurrentRepository.Entities;
            if (!string.IsNullOrEmpty(name)) _commonModels = _commonModels.Where(cm => cm.Name.Contains(name));
            if (userID > 0) _commonModels = _commonModels.Where(restaurant => restaurant.User_Id == userID);

            _commonModels = Order(_commonModels, orderCode);
            totalRecord = _commonModels.Count();
            return PageList(_commonModels, pageIndex, pageSize).AsQueryable();
        }

        public IQueryable<Restaurant> Order(IQueryable<Restaurant> entitys, int orderCode)
        {
            switch (orderCode)
            {
                //默认排序
                default:
                    entitys = entitys.OrderByDescending(cm => cm.Name);
                    break;
            }
            return entitys;
        }
    }
}
